import { Schema } from "koishi"

export const name = "怔群高考倒数"

export const inject = ["cron"]

export const Config = Schema.object({
  selfId: String,
  guildId: String,
})

export function apply(ctx, config) {
  const hex4 = n => n.toString(16).toUpperCase().padStart(4, "0")
  const update = async () => {
    const bot = ctx.bots["onebot:" + config.selfId]
    const countText1 = hex4(Math.floor((new Date(2026, 1 - 1, 6) - Date.now()) / 86400000) + 1)
    const countText2 = hex4(Math.floor((new Date(2026, 6 - 1, 7) - Date.now()) / 86400000) + 1)
    const currentName = (await bot.getGuild(config.guildId)).name
    const newName = currentName.replace(/(?<=\| )0x[ 0-9A-Fx-]+/, "0x" + countText1 + countText2)
    await bot.internal.setGroupName(config.guildId, newName)
  }

  ctx.cron("0 0 * * *", update)
}
